package com.example.search.thread;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class test {
    public static ExecutorService service = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("start");
//        CompletableFuture.runAsync(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("当前线程：" + Thread.currentThread().getId());
//                 int i = 10 / 2;
//                 System.out.println("运行结果：" + i);
//            }
//        }, service);

//        CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                System.out.println("当前线程：" + Thread.currentThread().getId());
//                     int i = 10 / 2;
//                     System.out.println("运行结果：" + i);
//                     return i;
//            }
//        },service).whenComplete(new BiConsumer<Integer, Throwable>() {
//            //虽然能得到异常信息，但是没法修改返回数据
//            @Override
//            public void accept(Integer integer, Throwable throwable) {
//                System.out.println("异步完成"+integer);
//            }
//        }).exceptionally(new Function<Throwable, Integer>() {
//            //可以感知异常，同时返回默认值
//            @Override
//            public Integer apply(Throwable throwable) {
//                return -1;
//            }
//        });

        /**
         * 方法执行完后端处理
         */
//         CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//             System.out.println("当前线程：" + Thread.currentThread().getId());
//             int i = 10 / 2;
//             System.out.println("运行结果：" + i);
//             return i;
//         }, service).handle((result,thr) -> {
//             if (result != null) {
//                 return result * 2;
//             }
//             if (thr != null) {
//                 System.out.println("异步任务成功完成了...结果是：" + result + "异常是：" + thr);
//                 return 0;
//             }
//             return 0;
//         });

        /**
         * 线程串行化
         * 1、thenRunL：不能获取上一步的执行结果
         * 2、thenAcceptAsync：能接受上一步结果，但是无返回值
         * 3、thenApplyAsync：能接受上一步结果，有返回值
         *
         */
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, service).thenApplyAsync(res -> {
//            System.out.println("任务2启动了..." + res);
//            return "Hello" + res;
//        }, service);
//        System.out.println("end");


        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程1：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果1：" + i);
            return i;
        }, service);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程2：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果2：" + i);
            return i;
        }, service);
        //两个都完成
        future1.thenAcceptBothAsync(future2, new BiConsumer<Integer, Integer>() {
            @Override
            public void accept(Integer integer, Integer integer2) {
                System.out.println(integer + integer2);
            }
        }, service);
        //
        future1.applyToEitherAsync(future2, new Function<Integer, Integer>() {
            @Override
            public Integer apply(Integer integer) {
                return null;
            }
        }, service);
    }

    /*public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("start");
        //1、继承thread
//        Thread1 thread = new Thread1();
//        thread.start();

        //2、实现runnable接口
//        Runnable1 runnable1 = new Runnable1();
//        Thread thread = new Thread(runnable1);
//        thread.start();

        //3.实现Callable接口+futuretask。可以获取返回结果，使用.get()方法
//         FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
//         new Thread(futureTask).start();
//         System.out.println(futureTask.get());

        //4.线程池ExecutorService。以上三种都不用，应该将所有任务交由线程池执行
//        new Thread(()->{
//            System.out.println("hello");
//        }).start();
        service.execute(new Runnable1());
        service.submit(new Callable01());

    }*/

    //1、继承thread
    public static class Thread1 extends Thread {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().toString());
            int i = 10 / 2;
            System.out.println(i);
        }
    }

    //2、实现runnable接口
    public static class Runnable1 implements Runnable {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().toString());
            int i = 10 / 2;
            System.out.println(i);
        }
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }
    }
}
